/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yjt.zdjf.standard.communicate.sftp.organization.client;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import com.yjt.common.exception.YJTCoreException;

/**
 * SFTP文件收发器
 * @author XuePT
 *TODO 异常回应及各返回结果更新
 */
@Component(SFTPTransceiver.PACKAGE_CLASS_NAME)
public class SFTPTransceiver {
	
	public static final String PACKAGE_CLASS_NAME = "com.yjt.zdjf.standard.communicate.sftp.organization.client.SFTPTransceiver";
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	SftpFilesUploadGateway sftpFilesUploadGateway;
	@Autowired
	SftpFilesDownloadGateway sftpFilesDownloadGateway;

	public Message<?> uploadFile(File fileToUpload,String remote_dir) throws YJTCoreException {
		try {
			Message<File> message = MessageBuilder.withPayload(fileToUpload).setHeader("remote_dir", remote_dir).build();
			return sftpFilesUploadGateway.uploadFiles(message);
		} catch(Exception e) {
			try {
				logger.info("[SFTP上传]文件(" + fileToUpload.getCanonicalPath() + ")上传异常", e);
			} catch (IOException e1) {
				logger.info("[SFTP上传]文件上传异常,获取文件路径失败", e);
				throw new YJTCoreException("[SFTP上传]文件(" + fileToUpload.getName() + ")上传异常", e);
			}
			throw new YJTCoreException("[SFTP上传]文件(" + fileToUpload.getName() + ")上传异常", e);
		}
	}

	public Message<?> downloadFile(String fileToDownload,String local_dir) throws YJTCoreException {
		try {
			Message<String> message = MessageBuilder.withPayload(fileToDownload).setHeaderIfAbsent("local_dir", local_dir).build();
			return sftpFilesDownloadGateway.downloadFiles(message);
		} catch(Throwable e) {
			//20151115出现有文件下载不成功，但是没输出错误日志，原来此处是捕获Exception的，但是有可能是Throwable
			logger.error("[SFTP下载]文件(" + fileToDownload + ")下载异常。", e);
			throw new YJTCoreException("[SFTP下载]文件下载异常。",e);
		}
	}

	
	public Message<?> uploadFiles(File filesToUpload,String remote_dir) throws YJTCoreException {
		try {
			Message<File> message = MessageBuilder.withPayload(filesToUpload).setHeader("remote_dir", remote_dir).build();
			return sftpFilesUploadGateway.uploadFiles(message);
		} catch(Exception e) {
			try {
				logger.info("[SFTP上传]文件(" + filesToUpload.getCanonicalPath() + ")上传异常", e);
			} catch (IOException e1) {
				logger.info("[SFTP上传]文件上传异常，获取文件路径失败。", e);
				throw new YJTCoreException("[SFTP上传]文件(" + filesToUpload.getName() + ")上传异常", e);
			}
			throw new YJTCoreException("[SFTP上传]文件(" + filesToUpload.getName() + ")上传异常", e);
		}
	}

	public Message<?> downloadFiles(String filesToDownload,String local_dir) throws YJTCoreException {
		try {
			Message<String> message = MessageBuilder.withPayload(filesToDownload).setHeader("local_dir", local_dir).build();
			return sftpFilesDownloadGateway.downloadFiles(message);
		}catch (Exception e) {
			throw new YJTCoreException("[SFTP下载]文件(" + filesToDownload + ")下载异常。",e);
		}
	}
}